# 废弃

import time
import numpy as np
from src.app.CadoHero import CadoHero
from src.app.SoundSwinger import SoundSwinger
# from src.hardware.Button import Button
import RPi.GPIO as GPIO
import threading
from utils.common import RESOURCE_DIR

from utils.zaudio import StdRecord
from utils.zplot import ZPlot, ZPlotEmpty

sr = StdRecord()
try:
    zplot = ZPlot(mode="wave")
    zplot.display(f"{RESOURCE_DIR}/start.jpg")
except OSError:
    # 小屏幕没有插好或插错位置
    zplot = ZPlotEmpty()

cado = CadoHero(zplot=zplot, with_midi_keyboard=True)
open_record = True  # 是否打开SoundSwinger


def OpenCadoHero():
    cado.start_midi_keyboard()
    

def main():
    cadohero_t = threading.Thread(target=OpenCadoHero)
    cadohero_t.start()

    pin = 18
    def key_callback(channel):
        global open_record
        open_record = not open_record
    GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
    GPIO.add_event_detect(pin, GPIO.FALLING, bouncetime=200)
    GPIO.add_event_callback(pin, callback=key_callback)
    
    while True:
        time.sleep(2)
        if open_record:
            for data in sr.get_sound_data_wait():
                data = np.frombuffer(data, dtype=np.int16)
                zplot.plot(data)
                if not open_record:
                    break
            

if __name__ == "__main__":
    main()
